package com.sline.smis.jdbc.dao.impl;

import com.sline.smis.jdbc.dao.StudentDao;
import com.sline.smis.jdbc.domain.Student;
import com.sline.smis.jdbc.template.JdbcTemplate;
import com.sline.smis.jdbc.template.handler.BeanListResultSetHandler;
import com.sline.smis.jdbc.template.handler.BeanResultSetHandler;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public void save(Student student) {
        String sql = "insert into t_student (name, age) values (?, ?)";

        int row = JdbcTemplate.update(sql, student.getName(), student.getAge());

        System.out.println("成功操作了" + row + "行数据");
    }

    @Override
    public void update(Long sid, Student student) {
        String sql = "update t_student set name = ?, age = ? where id = ?";

        int row = JdbcTemplate.update(sql, student.getName(), student.getAge(), sid);

        System.out.println("成功操作了" + row + "行数据");
    }

    @Override
    public void delete(Long sid) {
        String sql = "delete from t_student where id = ?";

        int row = JdbcTemplate.update(sql, sid);

        System.out.println("成功操作了" + row + "行数据");
    }

    @Override
    public Student get(Long sid) {
        String sql = "select * from t_student where id = ?";

        Student student = JdbcTemplate.query(sql, new BeanResultSetHandler<>(Student.class), sid);

        return student;
    }

    @Override
    public List<Student> list() {
        String sql = "select * from t_student";

        List<Student> arr = JdbcTemplate.query(sql, new BeanListResultSetHandler<>(Student.class));

        return arr;
    }
}
